<?php
/**
 * @package JFusion_Elgg
 * @author Arunoda Susiripala
 * @copyright Copyright (C) 2008 JFusion. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 */

// no direct access
defined('_JEXEC' ) or die('Restricted access' );

/**
 * JFusion Admin Class for Elgg
 * For detailed descriptions on these functions please check the model.abstractadmin.php
 * @package JFusion_Elgg
 */

class JFusionAdmin_elgg extends JFusionAdmin{

    function getJname(){
        return 'elgg';
    }

    function getTablename(){
        return 'users_entity';
    }

    function setupFromPath($forumPath){
        //check for trailing slash and generate file path
        if (substr($forumPath, -1) != DS) {
            $forumPath = $forumPath.DS;
        }
        
		$settingFile=$forumPath . engine . DS . "settings.php";
		//check to see whether $setting file available or not..
		if(!is_file($settingFile)){
			return;
		}
		$file=fopen($settingFile,"r");
	    while(!feof($file)){
			$var=fgets($file);
			$parts=split("=",$var);
			if(isset($parts[0]) && isset($parts[1])){
				$key=trim(preg_replace('/[^\n]*\$CONFIG->/ ', "",$parts[0]));
				$value=trim(str_replace(array('"',"'",";"), "",$parts[1]));
				$config[$key]=$value;
			}
		}
            //save the parameters into array
            $params = array();
            $params['database_host']     = $config['dbhost'];
            $params['database_name']     = $config['dbname'];
            $params['database_user']     = $config['dbuser'];
            $params['database_password'] = $config['dbpass'];
            $params['database_prefix']   = $config['dbprefix'];
            $params['database_type']     = "mysql";
            $params['source_path']       = $forumPath;
            return $params;
       
    }

    function getUserList(){
        //getting the connection to the db
        $db = JFusionFactory::getDatabase($this->getJname());
        $query = 'SELECT username, email from #__users_entity';
        $db->setQuery($query );

        //getting the results
        $userlist = $db->loadObjectList();

        return $userlist;
        
    }

    function getUserCount(){
        //getting the connection to the db
		$db = JFusionFactory::getDatabase($this->getJname());
        $query = 'SELECT count(*) from #__users_entity';
        $db->setQuery($query );

        //getting the results
        return $db->loadResult();
    }


    function getDefaultUsergroup(){
        //there is no usergroups in elgg
        
    	return "Default";  
    }
    
	function allow_empty_cookie_path(){
		return true;
	}

	function allow_empty_cookie_domain() {
		return true;
	}

	function debugConfigExtra(){
		
 	}

    function allowRegistration(){
    	$result = true;
       	$params = JFusionFactory::getParams($this->getJname());
		$registration_disabled = $params->get('disabled_registration');
		if ($registration_disabled){$result = false;}
		return $result;
    }

}